// client/src/components/Profile.tsx
import { useNavigate } from "react-router-dom";
import { ethers } from "ethers";
import { useEffect, useState } from "react";
import Navbar from "@/components/Navbar";

const Profile = () => {
  const navigate = useNavigate();
  const [ethBalance, setEthBalance] = useState("0");
  const [blockNumber, setBlockNumber] = useState(0);
  const [gasPrice, setGasPrice] = useState("0");
  const [account, setAccount] = useState("");

  useEffect(() => {
    const fetchNetworkData = async () => {
      try {
        // 连接MetaMask或其他provider
        const provider = new ethers.BrowserProvider(window.ethereum);
        
        // 获取账户地址
        const accounts = await provider.send("eth_requestAccounts", []);
        setAccount(accounts[0]);
        
        // 获取ETH余额
        const balance = await provider.getBalance(accounts[0]);
        setEthBalance(ethers.formatEther(balance));
        
        // 获取区块高度
        const block = await provider.getBlockNumber();
        setBlockNumber(block);
        
        // 获取Gas价格
        const gasPrice = await provider.getFeeData();
        setGasPrice(ethers.formatUnits(gasPrice.gasPrice || 0, "gwei"));
      } catch (error) {
        console.error("获取网络数据失败:", error);
      }
    };

    fetchNetworkData();
  }, []);

  return (
    <div className="flex h-screen flex-col">
      <Navbar />
      <div className="flex-1 overflow-auto bg-gray-50 p-6">
        <div className="max-w-7xl mx-auto">
          {/* 返回按钮 */}
          <button
            onClick={() => navigate(-1)}
            className="flex items-center text-gray-600 hover:text-gray-900 mb-2"
          >
            <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 mr-1" viewBox="0 0 20 20" fill="currentColor">
              <path fillRule="evenodd" d="M9.707 16.707a1 1 0 01-1.414 0l-6-6a1 1 0 010-1.414l6-6a1 1 0 011.414 1.414L5.414 9H17a1 1 0 110 2H5.414l4.293 4.293a1 1 0 010 1.414z" clipRule="evenodd" />
            </svg>
            返回
          </button>

          {/* 页面标题 */}
          <div className="mb-6">
            <h1 className="text-2xl font-bold text-gray-900">个人主页</h1>
            <p className="text-gray-600 mt-1">查看和管理您的区块链账户信息</p>
          </div>

          {/* 用户信息卡片 */}
          <div className="bg-white rounded-xl shadow-sm overflow-hidden mb-6">
            <div className="bg-primary p-6">
              <div className="flex items-center">
                <img 
                  src="https://picsum.photos/200/200" 
                  alt="用户头像" 
                  className="w-16 h-16 rounded-full object-cover border-4 border-white shadow-md" 
                />
                <div className="ml-4">
                  <h2 className="text-xl font-bold text-white">医生林</h2>
                  <p className="text-white/80 mt-1">医院审批员</p>
                </div>
                <div className="ml-auto">
                  <button 
                    onClick={() => navigate("/edit-profile")} 
                    className="bg-white/20 hover:bg-white/30 text-white px-4 py-2 rounded-md text-sm font-medium transition-all"
                  >
                    <i className="fa fa-edit mr-1"></i> 编辑资料
                  </button>
                </div>
              </div>
            </div>
            
            {/* 区块链账户信息 */}
            <div className="p-6">
              <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div className="bg-gray-50 rounded-lg p-4">
                  <h3 className="text-sm font-medium text-gray-500">账户余额</h3>
                  <p className="text-lg font-semibold text-gray-900 mt-1">{ethBalance} ETH</p>
                </div>
                <div className="bg-gray-50 rounded-lg p-4">
                  <h3 className="text-sm font-medium text-gray-500">区块高度</h3>
                  <p className="text-lg font-semibold text-gray-900 mt-1">{blockNumber.toLocaleString()}</p>
                </div>
                <div className="bg-gray-50 rounded-lg p-4">
                  <h3 className="text-sm font-medium text-gray-500">Gas价格</h3>
                  <p className="text-lg font-semibold text-gray-900 mt-1">{parseFloat(gasPrice).toFixed(0)} Gwei</p>
                </div>
              </div>
              
              <div className="mt-6">
                <h3 className="text-sm font-medium text-gray-500">账户地址</h3>
                <div className="mt-2 flex items-center justify-between bg-gray-50 rounded-lg p-4">
                  <div className="flex items-center text-sm font-medium text-gray-900 break-all">
                    {account || "未连接钱包"}
                  </div>
                  <button 
                    className="ml-2 rounded-md bg-gray-200 px-2 py-1 text-xs text-gray-700 hover:bg-gray-300"
                    onClick={() => account && navigator.clipboard.writeText(account)}
                  >
                    <i className="fa fa-copy mr-1"></i> 复制
                  </button>
                </div>
              </div>
              
              <div className="mt-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                  <h3 className="text-sm font-medium text-gray-500">交易数量</h3>
                  <p className="text-base text-gray-900 mt-1">42</p>
                </div>
                <div>
                  <h3 className="text-sm font-medium text-gray-500">网络</h3>
                  <span className="inline-flex items-center rounded-full bg-blue-100 px-2.5 py-0.5 text-xs font-medium text-blue-800">
                    本地测试网
                  </span>
                </div>
              </div>
            </div>
          </div>

          {/* 最近提交的表单 */}
          <div className="bg-white rounded-xl shadow-sm overflow-hidden">
            <div className="p-6 border-b border-gray-200">
              <h2 className="text-xl font-bold text-gray-900">最近提交的表单</h2>
              <p className="text-gray-600 mt-1">查看您最近提交的医疗记录申请</p>
            </div>
            
            <div className="p-6">
              <div className="overflow-x-auto">
                <table className="min-w-full divide-y divide-gray-200">
                  <thead className="bg-gray-50">
                    <tr>
                      <th scope="col" className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">姓名</th>
                      <th scope="col" className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">身份证号</th>
                      <th scope="col" className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">申请日期</th>
                      <th scope="col" className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">医疗类型</th>
                      <th scope="col" className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">状态</th>
                      <th scope="col" className="px-6 py-3 text-right text-xs font-medium uppercase tracking-wider text-gray-500">操作</th>
                    </tr>
                  </thead>
                  <tbody className="bg-white divide-y divide-gray-200">
                    <tr>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="flex items-center">
                          <div className="flex-shrink-0 h-10 w-10 rounded-full bg-gray-100 flex items-center justify-center">
                            <span className="text-gray-500 font-medium">张</span>
                          </div>
                          <div className="ml-4">
                            <div className="text-sm font-medium text-gray-900">张三</div>
                          </div>
                        </div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        110***********1234
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-05-20</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">门诊</td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <span className="inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800">
                          已批准
                        </span>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <button className="text-primary hover:text-primary-dark">查看详情</button>
                      </td>
                    </tr>
                    <tr>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="flex items-center">
                          <div className="flex-shrink-0 h-10 w-10 rounded-full bg-gray-100 flex items-center justify-center">
                            <span className="text-gray-500 font-medium">李</span>
                          </div>
                          <div className="ml-4">
                            <div className="text-sm font-medium text-gray-900">李四</div>
                          </div>
                        </div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        120***********5678
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-05-15</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">住院</td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <span className="inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800">
                          已批准
                        </span>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <button className="text-primary hover:text-primary-dark">查看详情</button>
                      </td>
                    </tr>
                    <tr>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="flex items-center">
                          <div className="flex-shrink-0 h-10 w-10 rounded-full bg-gray-100 flex items-center justify-center">
                            <span className="text-gray-500 font-medium">王</span>
                          </div>
                          <div className="ml-4">
                            <div className="text-sm font-medium text-gray-900">王五</div>
                          </div>
                        </div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        130***********9012
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-05-10</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">检查</td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <span className="inline-flex items-center rounded-full bg-red-100 px-2.5 py-0.5 text-xs font-medium text-red-800">
                          已拒绝
                        </span>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <button className="text-primary hover:text-primary-dark">查看详情</button>
                      </td>
                    </tr>
                    <tr>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="flex items-center">
                          <div className="flex-shrink-0 h-10 w-10 rounded-full bg-gray-100 flex items-center justify-center">
                            <span className="text-gray-500 font-medium">赵</span>
                          </div>
                          <div className="ml-4">
                            <div className="text-sm font-medium text-gray-900">赵六</div>
                          </div>
                        </div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        140***********3456
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">2025-04-28</td>
                      <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">手术</td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <span className="inline-flex items-center rounded-full bg-yellow-100 px-2.5 py-0.5 text-xs font-medium text-yellow-800">
                          待处理
                        </span>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <button className="text-primary hover:text-primary-dark">查看详情</button>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
              
              <div className="mt-6 flex justify-between items-center">
                <div className="text-sm text-gray-700">
                  显示 1-4 条，共 4 条
                </div>
                <div className="flex space-x-1">
                  <button className="rounded-md border border-gray-300 px-3 py-1 text-sm text-gray-700 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed" disabled>
                    上一页
                  </button>
                  <button className="rounded-md bg-primary px-3 py-1 text-sm text-white">1</button>
                  <button className="rounded-md border border-gray-300 px-3 py-1 text-sm text-gray-700 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed" disabled>
                    下一页
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Profile;